﻿var CategoryFunc = function (rHd) {
    var __hd = (typeof (rHd) == "string") ? rHd : "CategoryFunc";
    var __data = null;
    var __path = new Array();
    var __typeKey = null;
    var __box = null;
    var __selectMode = 0; //0:single, 1: multi
    var __editUrl = "Admin/Content/Category/Edit.aspx";

    var __buildList = function (rData) {
        var _str = new Array();
        if (rData.Id != "root") {//root
            _str.push('<div class="category_item" idx="' + i + '" id="' + rData.Id + '" parent="' + rData.ParentId + '">');
            _str.push('<div >');
            if (rData.Children != null && rData.Children.length > 0) {
                //_str.push('<input type="checkbox" disabled="disabled" name="cbx_category" value="'+rData.Id+'" />');
            } else {
                _str.push('<input onclick="' + __hd + '.onItemChecked(this,event)" type="checkbox" name="cbx_category" value="' + rData.Id + '" />');
            }
            _str.push(rData.Name + '</div>');
            _str.push('<div class="category_sub">');
            for (var i = 0; i < rData.Children.length; i++) {
                _str.push(__buildList(rData.Children[i]));
            }
            _str.push('</div>');
            _str.push('</div>');
        } else {
            for (var i = 0; i < rData.Children.length; i++) {
                _str.push(__buildList(rData.Children[i]));
            }
        }
        return _str.join('');
    };
    var __buildDdl = function (rData) {
        var _str = new Array();
        if (rData.Id != "root") {//root
            _str.push('<div class="category_item" idx="' + i + '" id="' + rData.Id + '" parent="' + rData.ParentId + '">');
            _str.push('<div >');
            if (rData.Children != null && rData.Children.length > 0) {
                //_str.push('<input type="checkbox" disabled="disabled" name="cbx_category" value="'+rData.Id+'" />');
            } else {
                _str.push('<input onclick="' + __hd + '.onItemChecked(this,event)" type="radio" name="cbx_category" value="' + rData.Id + '" />');
            }
            _str.push(rData.Name + '</div>');
            _str.push('<div class="category_sub">');
            for (var i = 0; i < rData.Children.length; i++) {
                _str.push(__buildDdl(rData.Children[i]));
            }
            _str.push('</div>');
            _str.push('</div>');
        } else {
            for (var i = 0; i < rData.Children.length; i++) {
                _str.push(__buildList(rData.Children[i]));
            }
        }
        return _str.join('');
    };

    var __buildGridView = function (rData) {
        var _str = new Array();
        _str.push('<table class="TreeGridView" cellspacing="0">');
        _str.push('<tr class="TreeGridViewHeader">');
        _str.push('<th>分类名称</th>');
        _str.push('<th>别名</th>');
        _str.push('<th>图片</th>');
        _str.push('<th>排序值</th>');
        _str.push('<th>等级</th>');
        _str.push('<th>ID</th>');
        _str.push('</tr>');
        if (rData.Children != null) {
            for (var i = 0; i < rData.Children.length; i++) {
                _str.push(__buildGridViewRow(rData.Children[i], 0));
            }
        }
        _str.push('</table>');
        return _str.join('');

    };

    var __buildGridViewRow = function (rData, rLayer) {
        var _str = new Array();
        _str.push('<tr class="TreeGridViewRow">')
        _str.push('<td><div style="padding-left:' + (rLayer * 20) + 'px">');
        if (rData.Id != "root") {//root
            _str.push('<input type="checkbox" name="cbx_category" value="' + rData.Id + '" />');
        }
        _str.push('<a href="' + __editUrl + '?id=' + rData.Id + '&tid=' + __typeKey + '&pid=' + rData.ParentId + '">' + rData.Name + '</a></div></td>');
        _str.push('<td>' + rData.Alias + '</td>');
        //CoverPath-------------------
        _str.push('<td width="16"><img onclick="showPicPanel(this,event)" pid="' + rData.Id + '" ');
        _str.push(' src="/Cellula_client/Default/Images/Product/icon_pic_sign1.png" ');
        _str.push(' width="16px" height="16px" alt="" />' + rData.PicCount + '</td>');
        //-----------------------------
        _str.push('<td>' + rData.Order + '</td>');
        if (rData.Level != null) {
            _str.push('<td>' + rData.Level + '</td>');
        } else {
            _str.push('<td></td>');
        }
        _str.push('<td>' + rData.Id + '</td>');
        _str.push('</tr>');
        rLayer++;
        for (var i = 0; i < rData.Children.length; i++) {
            _str.push(__buildGridViewRow(rData.Children[i], rLayer));
        }
        return _str.join('');
    };

    var __selectedItem = null;
    var __loadData = new Array();
    var __ajax = null;
    this.onItemClick = function (sender, e) {
        var item = sender.parentNode.parentNode;
        var subObj = item.childNodes[1];
        var idx = parseInt(item.getAttribute("idx"));
        var key = item.getAttribute("id");
        if (subObj.style.display == "none") {
            subObj.style.display = "block";
        } else {
            subObj.style.display = "none";
        }
        item = subObj = idx = key = null;
    };

    this.getListHtml = function (rData) {
        return __buildList(__data);
    };
    this.setData = function (v) {
        __data = v;
    };
    this.display = function (rData) {
        __data = rData;
        var _str = __buildList(__data);
        document.write(_str);
    };
    this.displayGridView = function (rBoxId, rData, rEditUrl) {
        __box = $("#" + rBoxId);
        __data = rData;
        if (typeof (rEditUrl) == "string" && rEditUrl.length > 0) {
            __editUrl = rEditUrl;
        }
        __box.innerHTML = __buildGridView(__data);
    };

    var __checkSub = function (rBox, rChecked) {
        if (rBox == null) { return; }
        if (rBox.tagName == "INPUT" && rBox.getAttribute("type") == "checkbox") {
            rBox.checked = rChecked;
        }
        if (rBox.childNodes != null && rBox.childNodes.length > 0) {
            for (var i = 0; i < rBox.childNodes.length; i++) {
                __checkSub(rBox.childNodes[i], rChecked);
            }
        }
    };

    this.onItemChecked = function (sender, e) {
        var subContainer = sender.parentNode.parentNode.childNodes[1];
        __checkSub(subContainer, sender.checked);
    };
    this.setTypeKey = function (v) {
        __typeKey = v;
    };

    //BEGIN: bind data -------------
    this.panel = new function () {
        var __requestUrl = "Ws/_Action.aspx?id=36e5fa7d72ba4fd38b8b987d0627251b&parms=";
        var __panel = null;
        var __panelId = jskitUtil.guid();
        var __panelBody = null;
        var __modelKey = null;
        var __panelInfo = null;
        var __panelTitle = null;
        var __title = null;
        var __buildPanel = function () {
            if (__panel == null) {
                __panel = document.createElement("div");
                $("body").appendChild(__panel);
                __panel.setAttribute("id", __panelId);
                __panel.className = "category_panel";
                __panel.style.position = "absolute";
                var _str = new Array();
                _str.push('<div class="category_panel_title">' + __title + '</div>');
                _str.push('<div class="category_body_loading"></div>');
                _str.push('<div class="category_panel_shadow">&nbsp;</div>');
                _str.push('<div class="category_action_bar">');
                _str.push('<div id="i_' + __panelId + '" class="category_info"></div>');
                _str.push('<div class="category_btn_group">');
                _str.push('<input class="category_action_btn" type="button" value="保存" onclick="' + __hd + '.panel.saveBind(this,event)" />');
                _str.push('&nbsp;');
                _str.push('<input class="category_action_btn" type="button" value="取消" onclick="' + __hd + '.panel.closePanel()" />');
                _str.push('&nbsp;');
                _str.push('</div>');
                _str.push('</div>');
                __panel.innerHTML = _str.join('');
                __panelTitle = __panel.childNodes[0];
                __panelBody = __panel.childNodes[1];
                __panelInfo = $("#i_" + __panelId);
                _str = null;
            }
        };
        var __ajax = null;
        var __cData = null;
        var __showPanel = function () {
            __buildPanel();
            __panelBody.innerHTML = "&nbsp;";
            __panelBody.className = "category_body_loading";
            __panel.style.display = "block";
            __panel.style.left = ((document.documentElement.clientWidth - __panel.offsetWidth) / 2 + document.documentElement.scrollLeft) + "px";
            __panel.style.top = ((document.documentElement.clientHeight - __panel.offsetHeight) / 2 + document.documentElement.scrollTop) + "px";
            __bindData();
        };
        var __stopBind = function () {
            __ajax = null;
            __cData = null;
        };
        this.closePanel = function () {
            __stopBind();
            __panel.style.display = "none";
        };
        this.setRequestUrl = function (v) {
            __requestUrl = v;
        };
        this.setModelKey = function (v) {
            __modelKey = v;
        };
        var __bindData = function (rPmData) {
            __ajax = new JskitXmlHttpAction(__requestUrl + __modelKey, __hd + ".panel.__bindDataCallback", "text");
            __panelInfo.innerHTML = "装载分类数据...";
        };
        this.__bindDataCallback = function (json) {
            __panelInfo.innerHTML = "";
            __ajax = null;
            __panelBody.className = "category_body";
            __panelBody.innerHTML = __buildList(__data);
            eval("__cData = " + json + ";");
            var nl = $("@cbx_category");
            if (nl == null) { return; }
            for (var i = 0; i < __cData.length; i++) {
                for (var j = 0; j < nl.length; j++) {
                    if (nl[j].value == __cData[i].MarketKey) {
                        nl[j].checked = true;
                        break;
                    }
                }
            }
            __panelInfo.innerHTML = "";
        };
        this.saveBind = function () {
            if (__modelKey == null || __modelKey == "") {
                __panelInfo.innerHTML = "ModelKey丢失，无法提交";
                return;
            }
            var _str = new Array();
            var nl = $("@cbx_category");
            for (var j = 0; j < nl.length; j++) {
                if (nl[j].checked) {
                    _str.push(nl[j].value);
                }
            }
            if (_str.length > 0) {
                var _url = "Ws/_ProductMarket.aspx?pid=" + __modelKey + "&mk=" + _str.join(',');
                __ajax = new JskitXmlHttpAction(_url, __hd + ".__saveBindCallback", "text");
                __panelInfo.innerHTML = "提交保存数据....";
            } else {
                __panelInfo.innerHTML = "请先选择设置项";
            }
        };
        this.__saveBindCallback = function (json) {
            __panelInfo.innerHTML = "数据处理中...";
            var _bk = null;
            try {
                eval("_bk = " + json + ";");
                __panelInfo.innerHTML = _bk.msg;
            } catch (e) { }
        };
        this.showPanel = function (rTitle) {
            __title = (typeof (rTitle) == "string") ? rTitle : "";
            __showPanel();
        };
    };
    //END: bind data ----------------

};
